package com.example.demo.thread.juc;

import java.util.concurrent.CountDownLatch;

/**
 * Description: <br/>
 *
 * @author liulonglong
 * @taskId <br/>
 * @Date 2019/11/20 16:37
 * @Version 1.0
 */
public class Driver {
    private static final int N = 10;

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch switcher = new CountDownLatch(1);

        for (int i = 0; i < N; ++i) {
            new Thread(new Worker(switcher)).start();
        }

        doSomething();
        switcher.countDown();       // 主线程开启开关

    }

    public static void doSomething() {
        System.out.println("前置操作，countDown()之前");
    }
}

class Worker implements Runnable {
    private final CountDownLatch startSignal;

    Worker(CountDownLatch startSignal) {
        this.startSignal = startSignal;
    }

    public void run() {
        try {
            startSignal.await();    //所有执行线程在此处等待开关开启
            doWork();
        } catch (InterruptedException ex) {
        }
    }

    void doWork() {
        System.out.println("dododooddoddoodod!");
    }
}
